<?php

namespace App\Http\Controllers\Banner;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Banner;
use App\Models\Tags;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\DB;
use Auth;
class BannerController extends Controller
{
	//20180716  banner列表
    public function index(){

    	$banner   = new Banner();
    	$wherearr = [];
    	$where    = [];
    	if (Input::has('keyword')){
    		$keyword = Input::get('keyword');
    		if(is_numeric($keyword)){
    			$where['id'] = $keyword;
    		}else{
    			$wherearr['title'] = $keyword;
    		}
		}
		if (Input::has('state')){
    		$where['state'] = Input::get('state');
		}

		$list = $banner->listinfo($where, $wherearr);

    	$this->ret['list'] = $list;
    	$this->ret['state']= Input::get('state');
    	$this->ret['keyword']= Input::get('keyword');

        return view('banner.index', $this->ret);
    }

     //20180716  添加banner页面
    public function add(Request $request){
    	

    	return view('banner.add');
    }
    //20180716 上传图片页面
    public function uploads(Request $request)
    {
        if ($request->ajax()) {
            $file = $request->file('image');
            // 第一个参数代表目录, 第二个参数代表我上方自己定义的一个存储媒介
            $path = $file->store('avatars/banner/'.date('Ymd'), 'uploads');
            return response()->json(array('msg' => $path));
        }
        // 注意看下方模版代码
        //return view('uploadimage');
    }

    //20180716 执行添加banner
    public function insertBanner(Request $request){
    	$banner = new Banner();
    	echo $banner->insert($request);

    }

    //20180716  编辑banner
    public function editBanner($id){
    	$banner = new Banner();
    	$item   = $banner->one($id);
    	$this->ret['item'] = $item[0];
    	return view("banner.edit", $this->ret);
    }

    //20180716 执行编辑banner
    public function updateBanner(Request $request){
    	$banner = new Banner();
    	echo $banner->updateone($request);
    }


    public function deleteBanner($id){
    	$banner = new Banner();
    	if($banner->deleteone($id)){
    		return redirect('/banner/list');
    	}
    }

    public function updateBannerState($id,$state){
    	$banner     = new Banner();
    	$item       = $banner::find($id);
    	$item->state = $state;

    	if($item->save()){
    		return redirect('/banner/list');
        }
    }

}
